<?php

namespace core\libs;

/**
 +------------------------------------------------------------------------------
 * 分页类 
 +------------------------------------------------------------------------------
 */
class page {
    private $pagesize;      //每页显示条数
    private $pageval;       //当前页
    private $url;           //请求URL
    private $listNum;       //数字列表个数
    private $limit;         //limit SQL语句
    private $numTotal;      //共多少条
    private $pageTotal;     //共多少页
    private $args = "";     //分页页面需要传递的参数
    
    public function __construct($numTotal, $pagesize = 10) {
        $this->pagesize = $pagesize ? $pagesize : 10;
        $this->pageval = isset($_REQUEST['page']) && $_REQUEST['page'] > 0 ? $_REQUEST['page'] : 1;
        $this->url = $this->getUrl();
        $this->listNum = 10;
        $this->limit = $this->getLimit();
        $this->numTotal = $numTotal;
        $this->pageTotal = ceil($this->numTotal/$this->pagesize);
    }
    public function __get($args) {
        if($args == "limit") return $this->limit;
        if($args == "pagesize") return $this->pagesize;
        if($args == "pagesizecount") return $this->numTotal;
        if($args == "pagecount") return (int)$this->pageTotal;
        return;
    }
    public function __set($key, $val) {
        if($key == "args" && $val) $this->$key = $val;
    }

    private function getUrl() {
        $url = rtrim($_SERVER['REQUEST_URI'],'/');
        $url = str_replace("/page/".$this->pageval,"", $url);
        $url = explode('?', $url);
        $url = $url[0];
        return $url;
    }
    private function getLimit() {
        return " limit ".($this->pageval-1)*$this->pagesize.",".$this->pagesize;
    }
    
    private function first() {
        if($this->pageval>1)
            return "<a href={$this->url}/page/1{$this->args}><<</a>";
    }
    private function prev() {
        if($this->pageval>1)
            return "<a href={$this->url}/page/".($this->pageval-1)."{$this->args}><</a>";
    }
    
    private function next() {
        if($this->pageval<$this->pageTotal)
            return "<a href={$this->url}/page/".($this->pageval+1)."{$this->args}>></a>";
    }
    private function last() {
        if($this->pageval<$this->pageTotal)
            return "<a href={$this->url}/page/{$this->pageTotal}{$this->args}>>></a>";
    }
    private function list() {
        $pagelist = "";
        $val = floor($this->listNum/2);
        for($i=$val;$i>=1;$i--) {
            $page = $this->pageval - $i;
            if($page<1) continue;
            $pagelist .= "<a href={$this->url}/page/{$page}{$this->args}>{$page}</a>";
        }
        $pagelist .= "<span>".$this->pageval."</span>";
        for($i=1;$i<=$val;$i++) {
            $page = $this->pageval + $i;
            if($page>$this->pageTotal) break;
            $pagelist .= "<a href={$this->url}/page/{$page}{$this->args}>{$page}</a>";
        }
        return $pagelist;
    }
    
    public function show() {
        $html = '';
        $html .= $this->first();
        $html .= $this->prev();
        $html .= $this->list();
        $html .= $this->next();
        $html .= $this->last();
        $html .= " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;页次：".$this->pageval."/".$this->pageTotal."页";
        $html .= " 每页".$this->pagesize."条";
        $html .= " 共".$this->numTotal."条";
        return $html;
    }

    public function show_style() {
        $html = '';
        if($this->pageval>1) {
            $html .= "<a class='prev-btn pager-btn' href={$this->url}/page/".($this->pageval-1)."{$this->args}><i>▲</i>  上一页</a>";
        }
            

        $pagelist = "<div class='list y-flex-row'>";
        $val = floor($this->listNum/2);
        for($i=$val;$i>=1;$i--) {
            $page = $this->pageval - $i;
            if($page<1) continue;
            $pagelist .= "<a class='number' href='{$this->url}/page/{$page}{$this->args}'>{$page}</a>";
        }
        $pagelist .= "<a class='number active'>".$this->pageval."</a>";
        for($i=1; $i<=$val; $i++) {
            $page = $this->pageval + $i;
            if($page>$this->pageTotal) break;
            $pagelist .= "<a class='number' href='{$this->url}/page/{$page}{$this->args}'>{$page}</a>";
        }
        $pagelist .= "</div>";
        $html .=$pagelist;



        if($this->pageval<$this->pageTotal) {
            $html .= "<a class='next-btn pager-btn' href={$this->url}/page/".($this->pageval+1)."{$this->args}>下一页<i>▲</i></a>";
        }

        return $html;
    }
}
